package com.terren.webdig.setting.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.terren.webdig.basic.impl.BasicServiceSupportImpl;
import com.terren.webdig.common.util.JsonUtil;
import com.terren.webdig.setting.entity.LoginUser;
import com.terren.webdig.setting.entity.Role;
import com.terren.webdig.setting.entity.UserRole;
import com.terren.webdig.setting.service.IUserConfigService;



@Service("userConfigService")
public class UserConfigServiceImpl extends BasicServiceSupportImpl implements IUserConfigService {
	private final String SQL_MAPPER = "setting.userConfigMapper.";

	@SuppressWarnings("unchecked")
	public List<LoginUser> getUserList(LoginUser loginUser) throws Exception {
		return (List<LoginUser>) basicDao.findForList(SQL_MAPPER + "getUserList", loginUser);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LoginUser> getUserTypeList(LoginUser loginUser)
			throws Exception {
		return (List<LoginUser>) basicDao.findForList(SQL_MAPPER + "getUserTypeList", loginUser);
	}

	public LoginUser getUserByIdAll(LoginUser loginUser) throws Exception {
		LoginUser result = getUserByIds(loginUser);
		@SuppressWarnings("unchecked")
		List<Role> list = (List<Role>) basicDao.findForList(SQL_MAPPER +"getRoleByUserId",loginUser);
		String string ="";
		for (int i = 0; i < list.size(); i++) {
			if (i!=list.size()-1) {
				string+=list.get(i).getRoleId()+",";
			}else {
				string += list.get(i).getRoleId();
			}

		}
		result.setRoleIds(string);
		result.setRolesList(list);

		return result;
	}
	//id获取用户
	@SuppressWarnings("unchecked")
	public LoginUser getUserByIds(LoginUser loginUser) throws Exception{
		List<LoginUser> list = (List<LoginUser>) basicDao.findForList(SQL_MAPPER + "getUser", loginUser);
		if (list!=null && list.size()>0) {
			return list.get(0);
		}else {
			return null;
		}
	}

	/**
	 * 添加角色及用户
	 */
	public String saveUser(LoginUser loginUser) throws Exception {
		basicDao.save(SQL_MAPPER + "saveUser", loginUser);
		Integer userId = loginUser.getUserId();
		String roleIds = loginUser.getRoleIds();
		/**
		 * 角色跟功能的对应关系
		 */
		if (!roleIds.isEmpty()) {
			List<UserRole> list = new ArrayList<UserRole>();
			String[] roleIdArray = roleIds.split(",");
			for (String roleId : roleIdArray) {
				UserRole roleUser = new UserRole();
				roleUser.setUserId(loginUser.getUserId());
				roleUser.setRoleId(Integer.parseInt(roleId));
				list.add(roleUser);
			}
			userId += saveUserRole(list);
		}
		return String.valueOf(userId);
	}
	/**
	 * 批量插入用户
	 */
	@Override
	public void saveUserList(List<LoginUser> list) throws Exception {
		basicDao.save(SQL_MAPPER + "saveUserList", list);
		//是否需要给角色权限 分配权限的话需是分配90 只操作报告
		List<UserRole> roleList = new ArrayList<UserRole>();	
		for (int i = 0; i < list.size(); i++) {
			//手动分配90的权限
			UserRole roleUser = new UserRole();
			roleUser.setUserId(list.get(i).getUserId());
			roleUser.setRoleId(90);
			roleList.add(roleUser);
		}
	}
	@Override
	public Integer saveUserRole(List<UserRole> list) throws Exception {
		return (Integer) basicDao.saveList(SQL_MAPPER + "saveUserRole", list);
	}
	
	@Override
	public Boolean saveUserRoleList(List<UserRole> list)  {
		boolean flag=true;
		
		try {
			
			basicDao.save(SQL_MAPPER + "saveUserRoleList", list);
		
		} catch (Exception e) {
			
			e.printStackTrace();
			flag=false;
		}
		return flag;
	}

	@SuppressWarnings("unchecked")
	public List<LoginUser> deleteUser(LoginUser loginUser) throws Exception {

		return (List<LoginUser>) basicDao.findForList(SQL_MAPPER + "deleteUser", loginUser);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LoginUser> getUserOption(LoginUser loginUser) throws Exception {
		return (List<LoginUser>) basicDao.findForList(SQL_MAPPER + "getUser", loginUser);
	}

	@Override
	public String editPassword(LoginUser loginUser) throws Exception {
		String resultJson = null;
		boolean isSuccess = false;
		String msg = "修改密码失败";
		try {
			LoginUser checkUser = (LoginUser)basicDao.findForObject(SQL_MAPPER + "passwordAuthentification", loginUser);

			if(checkUser != null){
				loginUser.setUserId(checkUser.getUserId());
				//修改
				basicDao.save(SQL_MAPPER + "editPassword", loginUser);
				isSuccess = true;
				msg = "修改密码成功！";
			}else{
				isSuccess = false;
				msg = "原始密码输入错误！";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		resultJson = JsonUtil.messageJson(isSuccess, msg);
		return resultJson;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LoginUser> isRepeat(LoginUser loginUser) throws Exception {
		return (List<LoginUser>) basicDao.findForList(SQL_MAPPER + "isRepeat", loginUser);
	}
	
}
